<?php
namespace App\Services\Payment\PayCore\wechat\lib;

use App\Services\Payment\PayCore\wechat\lib\WxPayData\WxPayBizPayUrl;

/**
 * 
 * 刷卡支付实现类
 * @author widyhu
 *
 */
class WxPayNativePay
{
    protected $weixin_config;
    protected $wxpayApi;

    function __construct($weixin_config)
    {
        $this->weixin_config = $weixin_config;
        $this->wxpayApi = new WxpayApi($this->weixin_config);
    }

    /**
     * 生成扫描支付URL,模式一
     *
     * @param $productId
     * @return string
     * @throws WxPayException
     */
	public function GetPrePayUrl($productId)
	{
		$biz = new WxPayBizPayUrl($this->weixin_config);
		$biz->SetProduct_id($productId);
		$values = $this->wxpayApi::bizpayurl($biz);
		$url = "weixin://wxpay/bizpayurl?" . $this->ToUrlParams($values);
		return $url;
	}

    /**
     * 参数数组转换为url参数
     *
     * @param $urlObj
     * @return string
     */
	private function ToUrlParams($urlObj)
	{
		$buff = "";
		foreach ($urlObj as $k => $v)
		{
			$buff .= $k . "=" . $v . "&";
		}
		
		$buff = trim($buff, "&");
		return $buff;
	}

    /**
     * 生成直接支付url，支付url有效期为2小时,模式二
     *
     * @param $input
     * @return 成功时返回，其他抛异常
     * @throws WxPayException
     */
	public function GetPayUrl($input)
	{
		if($input->GetTrade_type() == "NATIVE")
		{
			$result = $this->wxpayApi::unifiedOrder($input);
			return $result;
		}
	}
}